/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbd.crawler.fwk;

import java.util.HashMap;
import java.util.Iterator;
import java.util.ResourceBundle;

// TODO : object caching

/**
 *
 * @author Amogh
 */
public class BaseFactory {

    public static final int INDEX_KEY   = 0;
    public static final int INDEX_VALUE = 1;

    public static final String REPLACE_WITH_NOTHING = "";
    public static final String REGEX_SQUARE_BRACE   = "[\\[\\]]";

    public static final String COMMA = ",";
    public static final String HASH  = "#";
    public static final String DOT   = ".";

    protected String bundleBaseName = null;
    protected String baseMapConfig  = null;
    protected String basePackage    = null;

    protected HashMap hashMap = null;

    //--------------------------------------------------------------------------
    /** Javadoc here */
    protected void loadHashMap () {

        if ( bundleBaseName != null 
              && baseMapConfig != null
              && basePackage != null )
        {
            ResourceBundle bundle = ResourceBundle.getBundle ( bundleBaseName );

            String mapConfig      = bundle.getString ( baseMapConfig );
            String handlerPackage = bundle.getString ( basePackage );

            String [] keyValueList = mapConfig.split ( HASH );

            if ( keyValueList != null
                  && keyValueList.length > 0 )
            {
                for ( int i = 0; i < keyValueList.length; i++ ) {

                    if ( keyValueList[i] != null ) {

                        keyValueList[i] = keyValueList[i].replaceAll (
                                                                REGEX_SQUARE_BRACE,
                                                                REPLACE_WITH_NOTHING
                                                           );

                        String [] keyValuePair = keyValueList[i].split ( COMMA );

                        if ( keyValuePair != null
                              && keyValuePair.length == 2 )
                        {
                            String url       = keyValuePair[INDEX_KEY].trim();
                            String classname = keyValuePair[INDEX_VALUE].trim();

                            /* for debugging purpose */
                            //System.out.println("url : " + url + " classname : " + classname);

                            classname = handlerPackage.concat ( DOT.concat (classname) );

                            if ( hashMap == null ) {
                                hashMap = new HashMap();
                            }

                            hashMap.put (url, classname);
                        }
                    }
                }
            }
        }

    } // end function loadHashMap

    //--------------------------------------------------------------------------
    /** Javadoc here */
    protected Object getInstance ( String mapKey )
    throws ClassNotFoundException,
            InstantiationException,
            IllegalAccessException
    {
        Object instance = null;

        if ( mapKey != null ) {

            /* for debugging purpose
            System.out.println("MapKey : " + mapKey);
            */

            String objectClassname = (String) hashMap.get ( mapKey.trim() );

            if ( objectClassname != null ) {

                Class<?> objectClass = Class.forName ( objectClassname );

                instance = objectClass.newInstance();
            }
        }

        return instance;

    } // end function getInstance

    //--------------------------------------------------------------------------
    /** Javadoc here */
    protected String getMapKey ( String rawKey ) {

        String mapKey = null;

        if ( rawKey != null ) {

            Iterator keyIterator = hashMap.keySet().iterator();

            while ( keyIterator.hasNext() ) {

                String key = (String) keyIterator.next();

                if ( rawKey.contains (key) ) {

                    mapKey = key;

                    break;
                }
            }
        }

        return mapKey;

    } // end function getMapKey

    //--------------------------------------------------------------------------
    /** Javadoc here */
    protected boolean isMappingAvailable ( String mapKey ) {

        return ( hashMap.containsKey (mapKey)
                    && hashMap.containsValue ( hashMap.get (mapKey) ) );

    } // end function isMappingAvailable

} // end class BaseFactory
